import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.concurrent.ForkJoinTask;

public class WriterTask extends ChainTask {
    public WriterTask(int taskNumber) {
        super(taskNumber);
    }

    @Override
    protected ForkJoinTask<Void> forkTask() {
        return new WriterTask(getTaskNumber() + 1).fork();
    }

    @Override
    protected void doAction(int firstNumber, int lastNumber) throws InterruptedException {
        try (
            PrintWriter writer = new PrintWriter(
                    "res/numbers" + (
                            Loader.getNumberOfThreads() > 1 ? "." + getTaskNumber() : ""
                    ) + ".txt"
            )
        ) {
            for (int i = firstNumber; i <= lastNumber; i++) {
                writer.print(Loader.getQueue(getTaskNumber()).take());
            }

            writer.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
